package br.com.vpsa.previdordevendas.model.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

import br.com.vpsa.previdordevendas.model.exception.TendenciaFaturamentoExceptionException;

public class HttpUtils 
{

	public static String requestGetHttps( String endereco ) {
		try
		{
			URL url = new URL(endereco); 
			HttpsURLConnection connection =  (HttpsURLConnection) url.openConnection();
			connection.setRequestProperty("Request-Method", "GET");
			connection.setDoInput(true);  
			connection.setDoOutput(false);
			connection.setReadTimeout(300000);
			connection.connect();
			
			BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));  
			
			StringBuffer result = new StringBuffer();  
			String s;  
			while (null != ((s = br.readLine()))) {  
			    result.append(s);  
			}  
			
			br.close();
	
			return result.toString();
		}
		catch( Exception e )
		{
			throw new TendenciaFaturamentoExceptionException("Falha de conexão: "+ endereco, e);
		}
	}

	public static String requestGetHttpsAutenticada( String endereco ) 
	{
		try
		{
			endereco += endereco.indexOf("?") > -1 ? "" : "?";
			endereco += endereco.charAt(endereco.length()-1) == '?' ? "" : "&";
			endereco += "token="+SessionUtil.getToken().getAccess_token(); 
			
			URL url = new URL(endereco); 
			HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
			connection.setRequestProperty("Request-Method", "GET");
			connection.setDoInput(true);  
			connection.setDoOutput(false);
			connection.setReadTimeout(300000);

			connection.connect();
			
			BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));  
			
			StringBuffer result = new StringBuffer();  
			String s;  
			while (null != ((s = br.readLine()))) {  
			    result.append(s);  
			}  
			
			br.close();
	
			return result.toString();
		}
		catch( Exception e )
		{
			throw new TendenciaFaturamentoExceptionException("Falha de conexão: "+ endereco, e);
		}
	}
	
	public static String requestPostHttps( String endereco, String conteudo ) {
		try
		{
			URL url = new URL(endereco); 
			HttpsURLConnection connection =  (HttpsURLConnection) url.openConnection();
			connection.setRequestProperty("Request-Method", "POST");
			connection.setDoInput(true);  
			connection.setDoOutput(true);
			connection.setReadTimeout(300000);
			connection.connect();
			
			OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
		    writer.write(conteudo);
		    writer.flush();			
			
			BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));  
			
			StringBuffer result = new StringBuffer();  
			String s;  
			while (null != ((s = reader.readLine()))) {  
			    result.append(s);  
			}  
			
			writer.close();
			reader.close();
	
			return result.toString();
		}
		catch( Exception e )
		{
			throw new TendenciaFaturamentoExceptionException("Falha de conexão: "+ endereco, e);
		}
	}
	
	
}
